﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Gauss.ISP
{

    public static class ISP_Assert
    {

        private static MethodInfo assertMethod = null;
        private static readonly string MPI_NET_FULLNAME = "MPI, Version=1.1.0.0, Culture=neutral, PublicKeyToken=29b4a045737654fe";
        static ISP_Assert()
        {
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (var a in assemblies)
            {
                if (a.FullName == MPI_NET_FULLNAME)
                {
                    if (a.GetModule("MPI.dll").GetType("MPI.Unsafe").GetMethod("ISP_Assert") != null)
                    {
                        ISPLoaded = true;
                        assertMethod = a.GetModule("MPI.dll").GetType("MPI.Unsafe").GetMethod("ISP_Assert");
                    }
                    else
                    {
                        ISPLoaded = false;
                    }
                    break;
                }
            }
        }

        private static bool ISPLoaded
        {
            get;
            set;
        }

        public static void Assert(bool assertion)
        {
            if (!assertion)
            {
                if (ISPLoaded)
                {
                    assertMethod.Invoke(null, new object[] { assertion });
                }
                else
                {
                    System.Diagnostics.Debug.Assert(assertion);
                }
            }
        }
    }
}
